package cn.iocoder.yudao.module.system.convert;

import cn.iocoder.yudao.module.gov.dal.dataobject.resourcecatalog.ResourceCatalogDO;
import cn.iocoder.yudao.module.gov.dal.dataobject.resourcedirectory.ResourceDirectoryDO;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 政务资源数据转换器
 * 负责将接口返回的JSON数据转换为实体类对象
 */
@Component
public class GovResourceConverter {
    private static final Logger log = LoggerFactory.getLogger(GovResourceConverter.class);

    /**
     * 将JSON数组转换为目录树实体列表
     */
    public List<ResourceCatalogDO> convertCatalogList(JSONArray jsonArray) {
        List<ResourceCatalogDO> resultList = new ArrayList<>();
        if (jsonArray == null || jsonArray.isEmpty()) {
            log.warn("转换目录树数据：输入JSON数组为空");
            return resultList;
        }

        for (int i = 0; i < jsonArray.size(); i++) {
            try {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                ResourceCatalogDO catalogDO = new ResourceCatalogDO();

                // 基本信息映射
                catalogDO.setId(jsonObject.getLong("id"));
                catalogDO.setMenuName(jsonObject.getString("menuName"));
                catalogDO.setClassifySj(jsonObject.getString("classifySj"));
                catalogDO.setCatalogNumber(jsonObject.getInteger("catalogNumber"));
                catalogDO.setImputationNumber(jsonObject.getInteger("imputationNumber"));
                catalogDO.setApiNumber(jsonObject.getInteger("apiNumber"));
                catalogDO.setFileNumber(jsonObject.getInteger("fileNumber"));
                catalogDO.setTabNumber(jsonObject.getInteger("tabNumber"));
                catalogDO.setParentName(jsonObject.getString("parentName"));
                catalogDO.setMenuDesc(jsonObject.getString("menuDesc"));
                catalogDO.setDeptId(jsonObject.getString("deptId"));
                catalogDO.setDeptName(jsonObject.getString("deptName"));
                catalogDO.setReposeId(jsonObject.getString("reposeId"));
                catalogDO.setUpdataType(jsonObject.getString("updataType"));
                catalogDO.setReport(jsonObject.getInteger("report"));
                catalogDO.setMenuCode(jsonObject.getString("menuCode"));
                catalogDO.setUpdataTime(jsonObject.getString("updataTime"));
                catalogDO.setCreateTime(jsonObject.getString("createTime"));

                resultList.add(catalogDO);
            } catch (Exception e) {
                log.error("转换目录树数据第{}条失败", i, e);
                // 单条转换失败不影响整体，继续处理下一条
            }
        }

        log.info("目录树数据转换完成：共{}条，成功转换{}条", jsonArray.size(), resultList.size());
        return resultList;
    }

    /**
     * 将JSON数组转换为目录实体列表
     */
    public List<ResourceDirectoryDO> convertDirectoryList(JSONArray jsonArray) {
        List<ResourceDirectoryDO> resultList = new ArrayList<>();
        if (jsonArray == null || jsonArray.isEmpty()) {
            log.warn("转换目录数据：输入JSON数组为空");
            return resultList;
        }

        for (int i = 0; i < jsonArray.size(); i++) {
            try {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                ResourceDirectoryDO directoryDO = new ResourceDirectoryDO();

                // 基本信息映射
                directoryDO.setCatalogId(jsonObject.getLong("catalogId"));
                directoryDO.setMenuId(jsonObject.getLong("menuId"));
                directoryDO.setDataId(jsonObject.getLong("dataId"));
                directoryDO.setUpdateTime(jsonObject.getString("updateTime"));
                directoryDO.setCatalogName(jsonObject.getString("catalogName"));
                directoryDO.setShareProperty(jsonObject.getString("shareProperty"));
                directoryDO.setApplyNum(jsonObject.getInteger("applyNum"));
                directoryDO.setMatterId(jsonObject.getInteger("matterId"));
                directoryDO.setMatterName(jsonObject.getString("matterName"));
                directoryDO.setMatterCode(jsonObject.getString("matterCode"));
                directoryDO.setDeptName(jsonObject.getString("deptName"));
                directoryDO.setDeptId(jsonObject.getLong("deptId"));
                directoryDO.setIsPermit(jsonObject.getInteger("isPermit"));
                directoryDO.setViewNum(jsonObject.getInteger("viewNum"));
                directoryDO.setDataReportTime(jsonObject.getString("dataReportTime"));
                directoryDO.setDataUpdateTime(jsonObject.getString("dataUpdateTime"));
                directoryDO.setIsImputation(jsonObject.getInteger("isImputation"));
                directoryDO.setMissingStaus(jsonObject.getInteger("missingStaus"));
                directoryDO.setUnTimelyStatus(jsonObject.getInteger("unTimelyStatus"));
                directoryDO.setDataFileUpdateTimeUntimely(jsonObject.getInteger("dataFileUpdateTimeUntimely"));
                directoryDO.setCheckTime(jsonObject.getString("checkTime"));
                directoryDO.setCatalogCode(jsonObject.getString("catalogCode"));
                directoryDO.setTabNumber(jsonObject.getInteger("tabNumber"));
                directoryDO.setFileNumber(jsonObject.getInteger("fileNumber"));
                directoryDO.setApiNumber(jsonObject.getInteger("apiNumber"));
                directoryDO.setBusinessDomain(jsonObject.getString("businessDomain"));
                directoryDO.setSubjectType(jsonObject.getString("subjectType"));
                directoryDO.setSubjectFeature(jsonObject.getString("subjectFeature"));
                directoryDO.setIndustryClass(jsonObject.getString("industryClass"));
                directoryDO.setBusinessClass(jsonObject.getString("businessClass"));
                directoryDO.setManageObjClass(jsonObject.getString("manageObjClass"));
                directoryDO.setTopicClass(jsonObject.getString("topicClass"));
                directoryDO.setInfoCategoryClass(jsonObject.getString("infoCategoryClass"));
                directoryDO.setFromSystem(jsonObject.getString("fromSystem"));
                directoryDO.setIrfClass(jsonObject.getString("irfClass"));
                directoryDO.setIrfType(jsonObject.getString("irfType"));
                directoryDO.setSrcdb(jsonObject.getString("srcdb"));
                directoryDO.setIrCode(jsonObject.getString("irCode"));
                directoryDO.setTableEnname(jsonObject.getString("tableEnname"));
                directoryDO.setIrProvideCode(jsonObject.getString("irProvideCode"));

                resultList.add(directoryDO);
            } catch (Exception e) {
                log.error("转换目录数据第{}条失败", i, e);
                // 单条转换失败不影响整体，继续处理下一条
            }
        }

        log.info("目录数据转换完成：共{}条，成功转换{}条", jsonArray.size(), resultList.size());
        return resultList;
    }
}
